package com.idenger.modules.edu.mobileWeb.enroll;

import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.idenger.common.utils.StringUtils;
import com.idenger.common.web.BaseController;
import com.idenger.modules.edu.entity.common.BaseResponseMsg;
import com.idenger.modules.edu.entity.common.ResponseMsg;
import com.idenger.modules.edu.entity.course.EduCourse;
import com.idenger.modules.edu.entity.enroll.EduEnroll;
import com.idenger.modules.edu.entity.enrolldetails.EduEnrollDetails;
import com.idenger.modules.edu.entity.order.EduOrder;
import com.idenger.modules.edu.entity.project.EduProject;
import com.idenger.modules.edu.entity.userextra.EduUserExtra;
import com.idenger.modules.edu.mobileWeb.utils.pdf.component.PDFHeaderFooter;
import com.idenger.modules.edu.mobileWeb.utils.pdf.component.PDFKit;
import com.idenger.modules.edu.mobileWeb.utils.pdf.exception.PDFException;
import com.idenger.modules.edu.service.course.EduCourseService;
import com.idenger.modules.edu.service.enroll.EduEnrollService;
import com.idenger.modules.edu.service.enrolldetails.EduEnrollDetailsService;
import com.idenger.modules.edu.service.order.EduOrderDetailsService;
import com.idenger.modules.edu.service.project.EduProjectService;
import com.idenger.modules.edu.service.shopping.EduShoppingCartService;
import com.idenger.modules.edu.service.user.EduUserService;
import com.idenger.modules.edu.web.comm.Constant;
import com.idenger.modules.edu.web.utils.MakeOrderNum;
import com.idenger.modules.sys.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.idenger.common.persistence.Page;
import com.idenger.modules.edu.entity.order.EduOrderDetails;
import com.idenger.modules.edu.entity.user.EduUser;
import com.idenger.modules.edu.mobileWeb.utils.pdf.TemplateBO;
import com.idenger.modules.edu.service.order.EduOrderService;
import com.idenger.modules.edu.service.userextra.EduUserExtraService;
import com.idenger.modules.sys.utils.UserUtils;

@Controller
@RequestMapping(value = "${mobilePath}/edu/enroll/eduEnroll")
public class MEnrollController extends BaseController {
	@Autowired
	private EduOrderService eduOrderService;
	@Autowired
	private EduEnrollService eduEnrollService;
	@Autowired
	private EduEnrollDetailsService eduEnrollDetailsService;
	@Autowired
	private EduCourseService eduCourseService;
	@Autowired
	private EduUserService eduUserService;
	@Autowired
	private EduOrderDetailsService eduOrderDetailsServcie;
	@Autowired
	private EduUserExtraService eduUserExtraService;
	@Autowired
	private EduProjectService eduProjectService;
	@Autowired
	private EduShoppingCartService eduShoppingCartService;
	
	
	
	
	
	
	/***
	 * 报名
	 * @param eduOrder
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "enroll")
	public String enroll(@RequestBody EduOrder eduOrder, HttpServletRequest request, HttpServletResponse response, Model model){
		BaseResponseMsg baseResponseMsg;
		EduUser currentUser = (EduUser) request.getAttribute("currentUser");
		//购买前先判断，有没有已经购买过，如果其中有一条已经购买，则该次购买失败.
		boolean checkEnroll = checkEnroll(eduOrder, currentUser);
		if(!checkEnroll){
			baseResponseMsg = new BaseResponseMsg(new ResponseMsg("1", "error"));
			baseResponseMsg.addData("此次订单中含有已购买过的课程，请检查！");
			return renderString(response, baseResponseMsg);
		}
		//生成一个订单
		//计算金额
		String price = computePrice(eduOrder.getProjectList());
		BigDecimal javaPrice = new BigDecimal(price);
		BigDecimal jsPrice = new BigDecimal(eduOrder.getPrice());
		if(javaPrice.compareTo(jsPrice)==0){
			eduOrder.setUserid(currentUser.getId());
			eduOrder.setTradeNo(MakeOrderNum.makeOrderNum(Constant.ORDER_TRADENO_DEFAULT));
			eduOrder.setPrice(price);
			eduOrder.setPayStatus(Constant.ORDER_PAYSTATUS_NOTPAY);
			eduOrder.setOrderStatus(Constant.ORDER_STATUS_UNFINISHED);
			User user = UserUtils.getUser();
			eduOrder.setCreateBy(user);
			eduOrder.setUpdateBy(user);
			eduOrder.setCreateDate(new Date());
			eduOrderService.save(eduOrder);
			//生成订单的额外信息
			addEduEnrollDetails(eduOrder, currentUser);
			//生成订单的发票信息
			addEduOrderDetails(eduOrder, currentUser);
			//生成报名的信息
			addEduEnroll(eduOrder, currentUser);
			baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
			baseResponseMsg.addData(eduOrder);
			return renderString(response, baseResponseMsg);
		}else{
			baseResponseMsg = new BaseResponseMsg(new ResponseMsg("1", "error"));
			baseResponseMsg.addData("金额不对");
			return renderString(response, baseResponseMsg);
		}
	}
	
	
	
	
	/***
	 * 免费的课程的回调
	 * @param eduOrder
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "makeOrderNotMoney")
	public String makeOrderNotMoney(EduOrder eduOrder,HttpServletRequest request, HttpServletResponse response, Model model){
		BaseResponseMsg baseResponseMsg;
		EduUser currentUser = (EduUser) request.getAttribute("currentUser");
		EduOrder eduOrder2 = eduOrderService.get(eduOrder.getId());
		eduOrder2.setOrderStatus(Constant.ORDER_STATUS_COMPLETED);
		eduOrder2.setPayStatus(Constant.ORDER_PAYSTATUS_ALREADYPAY);
		eduOrderService.save(eduOrder2);
		//删除购物车的记录
		deleteShoppCart(eduOrder.getId(), eduOrder.getUserid());
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		baseResponseMsg.addData("购买成功");
		return renderString(response, baseResponseMsg);
	}
	
	
	
	
	
	/***
	 *删除购物车记录 
	 * @param orderId
	 * @param userId
	 */
	public void deleteShoppCart(String orderId,String userId){
		List<EduEnroll> list = eduEnrollService.findListForOrderId(orderId);
		for(EduEnroll eduEnroll :list){
			eduShoppingCartService.deleteShoppoCart(userId, eduEnroll.getCurriculumId());
		}
    }
	
	
	/***
	 * 检查此次订单中是否有已经购买过的课程
	 * @param eduOrder
	 * @param currentUser
	 * @return
	 */
	public boolean checkEnroll(EduOrder eduOrder,EduUser currentUser){
		String userId = currentUser.getId();
		List<EduProject> list = eduOrder.getProjectList();
		EduCourse eduCourse = new EduCourse();
		eduCourse.setEduUser(currentUser);
		List<EduCourse> courseList = eduCourseService.listByForEnrollAndUserId(eduCourse);
		for(EduProject eduProject :list){
				if(StringUtils.isNotBlank(eduProject.getId())){
					List<EduEnroll> eduEnrollsForPay = eduEnrollService.findwhethPayProject(eduProject.getId(), userId);
					List<EduCourse> courses =new ArrayList<>();
						courses = eduProject.getEduCourseList();
						for(EduCourse course :courses){
							for(EduEnroll eduEnroll :eduEnrollsForPay){
								if(course.getId().equals(eduEnroll.getCurriculumId())){
									return false;
								}
						}
					}
				 }
				//如果是课程购买，则如果购买记录中有本次购买的一条课程记录，则购买失败。
				 if(StringUtils.isBlank(eduProject.getId())){
					 List<EduCourse> courses = eduProject.getEduCourseList();
						for(EduCourse course :courses){
							for(EduCourse courseForPay :courseList){
								if(course.getId().equals(courseForPay.getId())){
									return false;
								}
							}
						}
				}
			}
		return true;
	}
	
	
	
	
	
	  /***
	   * 新增报名记录
	   * @param eduOrder
	   * @param currentUser
	   */
	public void addEduEnroll(EduOrder eduOrder,EduUser currentUser){
		List<EduProject> list = eduOrder.getProjectList();
		List<EduEnroll> eduEnrolls = new ArrayList<>();
		for(EduProject eduProject :list){
				if(StringUtils.isNotBlank(eduProject.getId())){
					List<EduCourse> courses =new ArrayList<>();
					courses = eduProject.getEduCourseList();
					for(EduCourse course :courses){
						EduEnroll eduEnroll = new EduEnroll();
						eduEnroll.setUserid(currentUser.getId());
						eduEnroll.setCurriculumId(course.getId());
						eduEnroll.setOrderId(eduOrder.getId());
						eduEnroll.setProjectId(eduProject.getId());
						eduEnroll.setBindStatus("0");
						eduEnroll.setStatus("0");
						eduEnroll.setCreateBy(UserUtils.getUser());
						eduEnroll.setUpdateBy(UserUtils.getUser());
						eduEnroll.setCreateDate(new Date());
						eduEnrolls.add(eduEnroll);
					}
				 }
				 if(StringUtils.isBlank(eduProject.getId())){
					 List<EduCourse> courses = eduProject.getEduCourseList();
						for(EduCourse course :courses){
							EduEnroll eduEnroll = new EduEnroll();
							eduEnroll.setUserid(currentUser.getId());
							eduEnroll.setCurriculumId(course.getId());
							eduEnroll.setOrderId(eduOrder.getId());
							eduEnroll.setBindStatus(Constant.ENROLL_BINDSTATUS_NOT);
							eduEnroll.setStatus(Constant.EDUENROLL_STATUS_NO);
							eduEnroll.setCreateBy(UserUtils.getUser());
							eduEnroll.setUpdateBy(UserUtils.getUser());
							eduEnroll.setCreateDate(new Date());
							eduEnrolls.add(eduEnroll);
						}
				}
			}
		eduEnrollService.saveList(eduEnrolls);
	}
	
	
	
	/***
	 * 生成订单的额外信息
	 * @param eduOrder
	 * @param currentUser
	 */
	public void addEduEnrollDetails(EduOrder eduOrder,EduUser currentUser){
		EduEnrollDetails details = eduOrder.getDetails();
		if(details != null){
			details.setOrderId(eduOrder.getId());
			details.setCreateBy(UserUtils.getUser());
			details.setUpdateBy(UserUtils.getUser());
			details.setCreateDate(new Date());
			eduEnrollDetailsService.save(details);
			EduUserExtra eduUserExtra = eduUserExtraService.getByUserId(currentUser.getId());
			if(eduUserExtra == null){
				eduUserExtra = new EduUserExtra();
			}
				eduUserExtra.setUserId(currentUser.getId());
				eduUserExtra.setHaveJobTitle(details.getHaveJobTitle());
				eduUserExtra.setJobTitleName(details.getJobTitleName());
				eduUserExtra.setJobTitleLevel(details.getJobTitleLevel());
				eduUserExtra.setEducationLevel(details.getEducationLevel());
				eduUserExtra.setGraduateCollege(details.getGraduateCollege());
				eduUserExtra.setMajor(details.getMajor());
				eduUserExtra.setCompanyName(details.getCompanyName());
				eduUserExtra.setCompanyNature(details.getCompanyNature());
				eduUserExtra.setIdCard(details.getIdCard());
				eduUserExtra.setIsShanghaiFamilyRegister(details.getIsShanghaiFamilyRegister());
				eduUserExtra.setCreateBy(UserUtils.getUser());
				eduUserExtra.setCreateDate(new Date());
				eduUserExtra.setUpdateBy(UserUtils.getUser());
				eduUserExtraService.save(eduUserExtra);
		}
	}
	
	/***
	 * 生成订单的发票信息
	 * @param eduOrder
	 * @param currentUser
	 */
	public void addEduOrderDetails(EduOrder eduOrder,EduUser currentUser){
		EduOrderDetails eduOrderDetails = eduOrder.getEduOrderDetails();
		if(eduOrderDetails!=null){
			eduOrderDetails.setOrderId(eduOrder.getId());
			eduOrderDetails.setCreateBy(UserUtils.getUser());
			eduOrderDetails.setUpdateBy(UserUtils.getUser());
			eduOrderDetails.setCreateDate(new Date());
			eduOrderDetailsServcie.save(eduOrderDetails);
		}
	}
	
	
	/***
	 * 计算金额
	 * @param orderId
	 * @param userId
	 * @return
	 */
	public String computePrice(List<EduProject> projectList){
		BigDecimal  price = new BigDecimal("0.00");
	     price =  price.setScale(2,BigDecimal.ROUND_HALF_DOWN);
	     //循环所有
		for(EduProject eduProject :projectList){
			if(StringUtils.isNotBlank(eduProject.getId()) ){//项目id不为空的表示购买的是项目
				List<EduCourse> allList = eduCourseService.findProjectContentCourse(eduProject.getId());
				List<EduCourse> projectForCourseList = eduProject.getEduCourseList();
				if(allList.size() == projectForCourseList.size()){//如果购买的项目下的课程和所有课程的数量相同，则证明是打包购买,则只需要累加项目的价格即可
					EduProject eduProject2 = eduProjectService.get(eduProject.getId());
					BigDecimal projectPrice = new BigDecimal(eduProject2.getPreferentialPrice());
					price = price.add(projectPrice);
				}else{//如果不相同，则需要根据实际购买的课程来累加价格
					for(EduCourse course :allList){
						for(EduCourse eduCourse :projectForCourseList){
							if(course.getId().equals(eduCourse.getId())){
								BigDecimal  coursePrice = new BigDecimal(course.getPreferentialPrice());
								price = price.add(coursePrice);
							}
						}
					}
				}
			}else{//项目为空的表示购买的是课程
				List<EduCourse> list = eduProject.getEduCourseList();
				if(list.size()>0){
					for(EduCourse course:list){
						EduCourse eduCourse = eduCourseService.get(course.getId());
						BigDecimal  coursePrice = new BigDecimal(eduCourse.getPreferentialPrice());
						price = price.add(coursePrice);
					}
				}
			}
		}
		return price.toString();
	}
	
	
	
	
	//报名之前如果他之前有信息则带出来传给前台
	@RequestMapping(value = "orderDetails")
	public String orderDetails(HttpServletRequest request, HttpServletResponse response, Model model){
		BaseResponseMsg baseResponseMsg;
		EduUser currentUser = (EduUser) request.getAttribute("currentUser");
		EduUser eduUser = eduUserService.get(currentUser.getId());
		EduUserExtra eduUserExtra = eduUserExtraService.getByUserId(currentUser.getId());
		EduEnroll eduEnroll = new EduEnroll();
		eduEnroll.setUserid(currentUser.getId());
		List<EduEnroll> eduEnrolls = eduEnrollService.findList(eduEnroll);
		EduEnroll eduEnroll2 = new EduEnroll();
		if(eduEnrolls.size()>0){
			eduEnroll2 = eduEnrolls.get(0);
		}
		EduEnrollDetails eduEnrollDetails = eduEnrollDetailsService.getByOrderId(eduEnroll2.getOrderId());
		if(eduEnrollDetails  != null){
			eduUserExtra = new EduUserExtra();
			eduUserExtra.setHaveJobTitle(eduEnrollDetails.getHaveJobTitle());
			eduUserExtra.setJobTitleName(eduEnrollDetails.getJobTitleName());
			eduUserExtra.setJobTitleLevel(eduEnrollDetails.getJobTitleLevel());
			eduUserExtra.setEducationLevel(eduEnrollDetails.getEducationLevel());
			eduUserExtra.setGraduateCollege(eduEnrollDetails.getGraduateCollege());
			eduUserExtra.setMajor(eduEnrollDetails.getMajor());
			eduUserExtra.setCompanyName(eduEnrollDetails.getCompanyName());
			eduUserExtra.setCompanyNature(eduEnrollDetails.getCompanyNature());
			eduUserExtra.setIsShanghaiFamilyRegister(eduEnrollDetails.getIsShanghaiFamilyRegister());
			eduUserExtra.setIdCard(eduEnrollDetails.getIdCard());
		}
		EduOrderDetails details = eduOrderDetailsServcie.findByOrderId(eduEnroll2.getOrderId());
		Map map = new HashMap<>();
		map.put("eduUserExtra", eduUserExtra);
		map.put("details", details);
		map.put("eduUser", eduUser);
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		baseResponseMsg.addData(map);
		return renderString(response, baseResponseMsg);
	}
	
	
	
	//判断其购买状态
	@RequestMapping(value = "enrollStatus")
	public String enrollStatus(EduEnroll eduEnroll,HttpServletRequest request, HttpServletResponse response, Model model){
		BaseResponseMsg baseResponseMsg;
		EduUser currentUser = (EduUser) request.getAttribute("currentUser");
		List<EduEnroll> list = eduEnrollService.findList(eduEnroll);
		if(list.size()>0){
			baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
			baseResponseMsg.addData("已经购买过了");
			return renderString(response, baseResponseMsg);
		}
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		baseResponseMsg.addData("可以放心购买");
		return renderString(response, baseResponseMsg);
	}
	
	/***
	 * 我的课程列表
	 * @param eduOrder
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "listMyCourse")
	public String listMyCourse(EduOrder eduOrder,HttpServletRequest request, HttpServletResponse response, Model model){
		BaseResponseMsg baseResponseMsg;
		//获取token                                                                        
		EduUser currentUser = (EduUser) request.getAttribute("currentUser");
		//查出这个用户买过哪些课程
		EduCourse course = new EduCourse();
		course.setEduUser(currentUser);
		Page<EduCourse> pageForCourse = eduCourseService.pageByForEnrollAndUserId(new Page<EduCourse>(request, response),course);
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		baseResponseMsg.addData(pageForCourse);
		return renderString(response, baseResponseMsg);
	}
	
	
	
	
	
	/***
	 *我的项目列表 
	 * @param eduOrder
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "listMyProject")
	public String listMyProject(EduOrder eduOrder,HttpServletRequest request, HttpServletResponse response, Model model){
		BaseResponseMsg baseResponseMsg;
		//获取token                                                                        
		EduUser currentUser = (EduUser) request.getAttribute("currentUser");
		//查出这个用户买过哪些项目
		EduProject eduProject2 =  new EduProject();
		eduProject2.setEduUser(currentUser);
		Page<EduProject> pageForProject = eduProjectService.GruopByForEnrollForUserId(new Page<EduProject>(request, response),eduProject2);
		List<EduProject> list = pageForProject.getList();
		List<EduProject> eduProjects = new ArrayList<>();
		for(EduProject eduProject :list){
			String isStatus =  endProjectStatus(eduProject,currentUser.getId());
			List<EduCourse> courses = eduCourseService.courseListByEnrollProjectId(eduProject.getId(), currentUser.getId());
			eduProject.setEduCourseList(courses);
//			eduProject.setEndStatus(isStatus);
			eduProjects.add(eduProject);
		}
		pageForProject.setList(eduProjects);
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		baseResponseMsg.addData(pageForProject);
		return renderString(response, baseResponseMsg);
	}
	
	/***
	 * 项目结项状态
	 * @param eduProject
	 * @param userId
	 * @return
	 */
	public String endProjectStatus(EduProject eduProject,String userId){
		EduEnroll eduEnroll2 = new EduEnroll();
		eduEnroll2.setUserid(userId);
		eduEnroll2.setProjectId(eduProject.getId());
		List<EduEnroll> list = eduEnrollService.findList(eduEnroll2);
		boolean isStatus = true;
		int over = 0;
		for(EduEnroll eduEnroll3 :list){
			if(!"1".equals(eduEnroll3.getStatus())){
				isStatus = false;
			}
			if("3".equals(eduEnroll3.getStatus())){
				over++;
			}
		}
		if(isStatus){
			return "1";
		}
		if(over==list.size()){
			return "3";
		}
		return "0";
	}
	
	
	
	//结课
	@RequestMapping(value = "endCourse")
	public String endCourse(EduCourse eduCourse,HttpServletRequest request, HttpServletResponse response, Model model){
		BaseResponseMsg baseResponseMsg;
		EduUser currentUser = (EduUser) request.getAttribute("currentUser");	
		EduEnroll eduEnroll2 = new EduEnroll();
		eduEnroll2.setUserid(currentUser.getId());
		eduEnroll2.setCurriculumId(eduCourse.getId());
		EduEnroll eduEnroll = eduEnrollService.getEduEnrollByUserIdAndCourseId(eduEnroll2);
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		if(StringUtils.isNotBlank(eduEnroll.getStatus()) && Constant.EDUENROLL_STATUS_IS_OK.equals(eduEnroll.getStatus())){
			eduEnroll.setStatus(Constant.EDUENROLL_STATUS_YES);
			eduEnrollService.save(eduEnroll);
			baseResponseMsg.addData("result","结课成功");	
		}else{
			baseResponseMsg.addData("result","结课失败");
		}
		return renderString(response, baseResponseMsg);
	}
	//学分证明
	@RequestMapping(value = "creditCertificate")
	public String creditCertificate (EduCourse eduCourse,HttpServletRequest request, HttpServletResponse response, Model model){
		BaseResponseMsg baseResponseMsg;
		EduUser currentUser = (EduUser) request.getAttribute("currentUser");
//		EduUser currentUser = new EduUser();
//		currentUser.setId("ae135f58cb034ab2b75abd762146c131");
		eduCourse.setEduUser(currentUser);
		Page<EduCourse> page = eduCourseService.creditCertificate(new Page<EduCourse>(request, response),eduCourse);
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		baseResponseMsg.addData(page);
		return renderString(response, baseResponseMsg);
	}
	
		/***
		 * 导出pdf
		 * @param eduCourse
		 * @param request
		 * @param response
		 * @param model
		 */
	@RequestMapping(value = "downloadPdf")
	public void downloadPdf(EduCourse eduCourse,HttpServletRequest request, HttpServletResponse response, Model model){
		BaseResponseMsg baseResponseMsg;
		String userId = request.getParameter("userId");
		EduUser currentUser = eduUserService.get(userId);
		String courseIds = request.getParameter("courseIds");
		TemplateBO bo = dataEncapsulation(currentUser,courseIds);
		try {
	    	response.setHeader("content-disposition", "inline;filename="  
	                + URLEncoder.encode("学分证明.pdf", "UTF-8"));
			 createPDF(bo,"course.pdf",response);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	
	
	public TemplateBO dataEncapsulation(EduUser eduUser,String courseIds){
		TemplateBO bo = new TemplateBO();
		EduUser user = eduUserService.get(eduUser.getId());
		bo.setEduUser(user);
		Date newDate = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy");  
	    String dateString = formatter.format(newDate);
	    bo.setNowDate(dateString);
		String[] courseIdArray = courseIds.split(",");
		List<EduCourse> list = new ArrayList<>();
		for(String courseId :courseIdArray){
			EduCourse course = eduCourseService.listByForEnrollAndCourseId(courseId,eduUser.getId());
			list.add(course);
		}
		bo.setCourses(list);
		return bo;
	}
	
	
	
	
	
	
	public  void createPDF(Object data, String fileName,HttpServletResponse response){
	    //pdf保存路径
	    try {
	        //设置自定义PDF页眉页脚工具类
	        PDFHeaderFooter headerFooter=new PDFHeaderFooter();
	        PDFKit kit=new PDFKit();
	        kit.setHeaderFooterBuilder(headerFooter);
	        kit.exportToResponse(fileName,data,response);
	    } catch (Exception e) {
	        e.printStackTrace();
	        throw new PDFException("PDF文件生成异常",e);
	    }
	}
}
